<HTML> 
<HEAD> 
	<TITLE>3rd Party ROBOTC Drivers</TITLE> 
<link href="tabs.css" rel="stylesheet" type="text/css"/> 
<link href="doxygen.css" rel="stylesheet" type="text/css"/> 
</HEAD> 
 
<BODY bgcolor="#ffffff" link="#000000" vlink="#000000"> 
<table width="100%" bgcolor="navy" cellspacing=0 cellpadding=1 border=0> 
<tr><td><table width="100%" bgcolor="#EEEEDD" cellspacing=0 cellpadding=3 border=0> 
<tr> 
    <td width="33%" align="left"> <img src="images/LOGO_NXT.gif" width=266 height=44 border="0" alt="Mindstorms"></td> 
    <td><b><font size="+3" color="navy">3rd Party ROBOTC Drivers</font></b></td> 
    <td align="right"> <img src="images/logo.png" width=44 height=44 border="0" alt="RobotC"></td>     
</tr> 
  <tr bgcolor="#cccc99" align="right"> 
          <td colspan=3> <font face="arial" size="-1"> [<a href=main.html>Home</a>] [<a target=_top href="https://sourceforge.net/projects/rdpartyrobotcdr/">Download</a>] [<a target=_top href="http://apps.sourceforge.net/mantisbt/rdpartyrobotcdr/my_view_page.php">Submit a bug/suggestion</a>]  [<a target=_top href="http://www.robotc.net/forums/">ROBOTC Forums</a>] [<a target=_top href="http://botbench.com">Blog</a>] [<a target=_top href="http://sourceforge.net/donate/index.php?group_id=257238">Support this project</a>]</font> </td> 
</tr> 
</table></td></tr> 
</table> 
<!-- Generated by Doxygen 1.7.2 -->
<div class="header">
  <div class="headertitle">
<h1>mindsensors-powermeter-test1.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma config(Sensor, S1,     MSPOWER,             sensorI2CCustomFastSkipStates)</span>
<span class="preprocessor"></span><span class="comment">//*!!Code automatically generated by &#39;ROBOTC&#39; configuration wizard               !!*//</span>

<span class="comment">/*</span>
<span class="comment"> * $Id: mindsensors-powermeter-test1.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * mindsensors-powermeter.h provides an API for the Mindsensors Power Meter Sensor. This program</span>
<span class="comment"> * demonstrates how to use that API.</span>
<span class="comment"> *</span>
<span class="comment"> * Changelog:</span>
<span class="comment"> * - 0.1: Initial release</span>
<span class="comment"> *</span>
<span class="comment"> * Credits:</span>
<span class="comment"> * - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this.</span>
<span class="comment"> *</span>
<span class="comment"> * License: You may use this code as you wish, provided you give credit where it&#39;s due.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. </span>
<span class="comment"></span>
<span class="comment"> * Xander Soldaat (xander_at_botbench.com)</span>
<span class="comment"> * 16 December 2009</span>
<span class="comment"> * version 0.1</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;<a class="code" href="mindsensors-powermeter_8h.html" title="Mindsensors Power Meter Sensor.">drivers/mindsensors-powermeter.h</a>&quot;</span>

<span class="keywordtype">int</span> voltage = 0;
<span class="keywordtype">int</span> current = 0;
<span class="keywordtype">bool</span> scaleChanged = <span class="keyword">false</span>;
<span class="keywordtype">int</span> vscale = 0;
<span class="keywordtype">int</span> cscale = 0;

<span class="keyword">typedef</span> <span class="keyword">enum</span> {
  showvoltage,
  showcurrent
} tDisplayUnit;

tDisplayUnit displayUnit;
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * Wait until no button is pressed. Also known as debouncing.</span>
<span class="comment"> */</span>
<span class="keywordtype">void</span> debounce() {
  <span class="keywordflow">while</span>(nNxtButtonPressed != kNoButton) {
     wait1Msec(1);
  }
}

<span class="keywordtype">void</span> doDrawPoint(<span class="keywordtype">int</span> top, <span class="keywordtype">int</span> x_pos, <span class="keywordtype">int</span> y_pos) {
        nxtEraseRect(x_pos, 63, x_pos+5, 0);
        <span class="keywordflow">if</span> (displayUnit == showcurrent)
          nxtDisplayCenteredTextLine(7, <span class="stringliteral">&quot;Max: %d mA&quot;</span>, top);
        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (displayUnit == showvoltage)
          nxtDisplayCenteredTextLine(7, <span class="stringliteral">&quot;Max: %d mV&quot;</span>, top);
        nxtDisplayStringAt(0, 12, <span class="stringliteral">&quot;0&quot;</span>);
        nxtDrawLine(10, 10, 10, 60);
        nxtDrawLine(10, 10, 15, 10);
        nxtDrawLine(10, 35, 15, 35);
        nxtDrawLine(10, 60, 15, 60);

        nxtSetPixel(x_pos, y_pos);
}

<span class="keywordtype">int</span> scaleToTop() {
  <span class="comment">// scales (amps)</span>
  <span class="comment">// 0: 0 -   50 mA</span>
  <span class="comment">// 1: 0 -  100 mA</span>
  <span class="comment">// 2: 0 -  500 mA</span>
  <span class="comment">// 3: 0 - 1000 mA</span>
  <span class="comment">// 4: 0 - 3000 mA</span>

  <span class="comment">// scales (volts)</span>
  <span class="comment">// 0: 0 -   100 mV</span>
  <span class="comment">// 1: 0 -   500 mV</span>
  <span class="comment">// 2: 0 -  1000 mV</span>
  <span class="comment">// 3: 0 -  5000 mV</span>
  <span class="comment">// 4: 0 - 13500 mV</span>
  <span class="keywordflow">if</span> (displayUnit == showcurrent) {
          <span class="keywordflow">switch</span> (cscale) {
            <span class="keywordflow">case</span> 0: <span class="keywordflow">return</span> 50; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 1: <span class="keywordflow">return</span> 100; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 2: <span class="keywordflow">return</span> 500; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 3: <span class="keywordflow">return</span> 1000; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 4: <span class="keywordflow">return</span> 3000; <span class="keywordflow">break</span>;
          }
        } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (displayUnit == showvoltage) {
          <span class="keywordflow">switch</span> (vscale) {
            <span class="keywordflow">case</span> 0: <span class="keywordflow">return</span> 100; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 1: <span class="keywordflow">return</span> 500; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 2: <span class="keywordflow">return</span> 1000; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 3: <span class="keywordflow">return</span> 5000; <span class="keywordflow">break</span>;
            <span class="keywordflow">case</span> 4: <span class="keywordflow">return</span> 13500; <span class="keywordflow">break</span>;
          }
  }
  <span class="keywordflow">return</span> 0;
}

task displayCardio () {
  <span class="keywordtype">int</span> top = 0;
  <span class="keywordtype">int</span> y_val = 0;
  eraseDisplay();
  <span class="keywordflow">while</span> (<span class="keyword">true</span>) {
    <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 10; i &lt; 99; i++) {
      top = scaleToTop();
      <span class="keywordflow">if</span> (displayUnit == showcurrent) {
        y_val = (((long)current * 50) / top) + 10;
        <span class="keywordflow">if</span> (current &gt; top) {
          cscale++;
          scaleChanged = <span class="keyword">true</span>;
        }
      } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (displayUnit == showvoltage) {
        y_val = (((long)voltage * 50) / top) + 10;
        <span class="keywordflow">if</span> (voltage &gt; top) {
          vscale++;
          scaleChanged = <span class="keyword">true</span>;
        }
      }
      <span class="keywordflow">if</span> (scaleChanged) {
        eraseDisplay();
        scaleChanged = <span class="keyword">false</span>;
        i = 100;
      } <span class="keywordflow">else</span> {
              doDrawPoint(top, i, y_val);
              wait1Msec(20);
            }
    }
  }
}

task main {
  nNxtButtonTask = -2;
  nNxtExitClicks = 3;

  nxtDisplayCenteredTextLine(0, <span class="stringliteral">&quot;Mindstorms&quot;</span>);
  nxtDisplayCenteredBigTextLine(1, <span class="stringliteral">&quot;POWER&quot;</span>);
  nxtDisplayCenteredTextLine(3, <span class="stringliteral">&quot;Scope&quot;</span>);
  nxtDisplayCenteredTextLine(5, <span class="stringliteral">&quot;Connect MSPM to&quot;</span>);
  nxtDisplayCenteredTextLine(6, <span class="stringliteral">&quot;to S1&quot;</span>);
  wait1Msec(2000);

  StartTask(displayCardio);
  <span class="keywordflow">while</span> (<span class="keyword">true</span>) {
    <span class="comment">// Using the function below is a much efficient way of fetching both</span>
    <span class="comment">// voltage and current info.  You could also fetch them separately.</span>
    <span class="keywordflow">if</span> (!<a name="a0"></a><a class="code" href="group__mspm.html#gab3c4743d76fedc8e99d899a97097c349">MSPMreadVoltageCurrent</a>(MSPOWER, voltage, current)) {
      StopTask(displayCardio);
      wait1Msec(100);
      PlaySound(soundException);
      eraseDisplay();
      nxtDisplayCenteredBigTextLine(2, <span class="stringliteral">&quot;ERROR!&quot;</span>);
      nxtDisplayCenteredTextLine(4, <span class="stringliteral">&quot;Couldn&#39;t read&quot;</span>);
      nxtDisplayCenteredTextLine(4, <span class="stringliteral">&quot;from sensor&quot;</span>);
      wait1Msec(2000);
      StopAllTasks();
    }



    <span class="keywordflow">switch</span>(nNxtButtonPressed) {
      <span class="keywordflow">case</span> kEnterButton:
        displayUnit = (displayUnit == showvoltage) ? showcurrent : showvoltage;
        scaleChanged = <span class="keyword">true</span>;
        debounce();
        <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> kRightButton:
        <span class="keywordflow">if</span> (displayUnit == showcurrent) {
                <span class="keywordflow">if</span> (cscale == 4) cscale = 0;
                <span class="keywordflow">else</span> cscale++;
              } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (displayUnit == showvoltage) {
                                        <span class="keywordflow">if</span> (vscale == 4) vscale = 0;
                                        <span class="keywordflow">else</span> vscale++;
                                }
                                scaleChanged = <span class="keyword">true</span>;
        debounce();
        <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> kLeftButton:
        <span class="keywordflow">if</span> (displayUnit == showcurrent) {
                <span class="keywordflow">if</span> (cscale == 0) cscale = 4;
                <span class="keywordflow">else</span> cscale--;
              } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (displayUnit == showvoltage) {
                                        <span class="keywordflow">if</span> (vscale == 0) vscale = 4;
                                        <span class="keywordflow">else</span> vscale--;
                                }
        scaleChanged = <span class="keyword">true</span>;
        debounce();
        <span class="keywordflow">break</span>;
    }
    wait1Msec(1);
  }
}

<span class="comment">/*</span>
<span class="comment"> * $Id: mindsensors-powermeter-test1.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Mar 13 2013 19:56:42 for ROBOTC Drivers by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
